package com.example.demo.config.ExpressionRoot;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * 自定义权限验证
 */
@Component(value = "xp")
public class AuthExpressionRoot {

    public boolean hasAnyAuthorityName(String... authority){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();

        for(GrantedAuthority grantedAuthority: authorities){
            for(String s: authority){
                SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(s);
                if(grantedAuthority.equals(simpleGrantedAuthority)){
                    return true;
                }
            }
        }
        return false;
    }
}